#include "hyper_plane.h"
#include "iostream"

vector<Divide_Info> Hyper_Plane::divide(Data_Sample** data_sample, int sample_length, int subset_size)
{
	double* rank_value = new double[sample_length];
	for(int i = 0; i<sample_length;i++)
	{
		rank_value[i] =0;
	}
	for(int i = 0;i<sample_length;i++)
	{
		Data_Sample * sample = data_sample[i];
		Data_Node* vector = sample->data_vector;
		int length = sample->data_vector_length;
		for(int j = 0; j< sample->data_vector_length;j++)
		{
			rank_value[i] += vector[j].value;
		}
	}	
	for(int i = 0;i<sample_length;i++)
	{
		for(int j =i+1;j<sample_length;j++)
		{
			if(rank_value[i]>rank_value[j])
			{
				double temp = rank_value[i];
				rank_value[i] = rank_value[j];
				rank_value[j] = temp;
				Data_Sample* p_temp = data_sample[i];
				data_sample[i] = data_sample[j];
				data_sample[j] = p_temp;
			}
		}
	}
	delete[] rank_value;
	int subset_num =(int)(sample_length/(double)subset_size+0.5);
	double average_size = sample_length/(double)subset_num;
	int count = 0;
	Divide_Info* d_info = new Divide_Info();
	for(int i = 0;i<subset_num-1;i++)
	{
		d_info->start_offset = count;
		d_info->end_offset =(int)(average_size*(i+1)-1);
		d_info->length = d_info->end_offset-d_info->start_offset+1;
		count = d_info->end_offset+1;
		divide_info.push_back(*d_info);
	}
	d_info->start_offset = count;
	d_info->end_offset = sample_length-1;
	d_info->length = d_info->end_offset - d_info->start_offset +1;
	divide_info.push_back(*d_info);
	delete d_info;
	return divide_info;
}
vector<Divide_Info> Hyper_Plane::divide(Data_Sample** data_sample, int sample_length){
  vector<Divide_Info> div;
  div.clear();
  int i;
  for (i=0;i<sample_length;i++){
    Divide_Info di;
    di.start_offset=i;
    di.end_offset=i;
    di.length=1;
    div.push_back(di);
  }
  return div;
}
